<?php
/*
 *author:Yangligao
 *Date:2013-10-2
 */
require 'Config.php';
require 'Public/Include/adv.php';
require 'Public/Include/footer.php';
require 'Public/Include/search.php';
$smarty->assign('positionFlag',"我的购物车");
$flag = $_GET['flag'];

if($flag == 'display'){
	if(empty($_POST['food_id']) && empty($_POST['food_num'])){
		$food_id = isset($_GET['food_id'])?$_GET['food_id']:'';
		$food_num = 1;
	}else{
		$food_id = $_POST['food_id'];
		$food_num = $_POST['food_num'];
	}
	$arrOld = array();
	//如果COOKIE[''myproduct]存在需要保留里面的值
	if(isset($_COOKIE['myproduct'])){
		if(!empty($_COOKIE['myproduct'])){
			$arrOld = unserialize($_COOKIE['myproduct']);
		}
	}
	if(!empty($food_id) && !empty($food_num)){
		
		$arrOld["$food_id"] = $food_num;
		setcookie('myproduct',serialize($arrOld),time()+3600*24,'/');
	}
	$totalPrice=0;
	$tArr = array();
	foreach($arrOld as $key=>$value){
		//通过$key的值获取产品信息
		$foodModel = new FoodModel();
		$res = $foodModel->food_select('food_id,food_name,food_pic,food_singleprice,food_userprice',"where food_id={$key}");
		$res[0]['food_num'] = $value ;//在返回的数组中追加一个购买数量
		$tArr[] = $res[0];
		if(isset($_SESSION['user_name'])){
			$totalPrice = $totalPrice + $res[0]['food_num']*$res[0]['food_userprice'];
		}else{
			$totalPrice = $totalPrice + $res[0]['food_num']*$res[0]['food_singleprice'];
		}
	}
	//将总价赋值
	$smarty->assign('totalPrice',$totalPrice);
	$smarty->assign('buyNowRes',$tArr);
	$smarty->display('Index/shoppingcar.html');
	
}

?>